package demo.web;

import demo.dao.impl.UserMapperImpl;
import demo.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int id= Integer.parseInt(request.getParameter("id"));
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        System.out.println(id);
        UserMapperImpl userMapper=new UserMapperImpl();
        if(userMapper.updateUser(new User(id,name,password))==1){
            response.getWriter().print("修改成功");
        }else{
            response.getWriter().print("修改失败");
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id= Integer.parseInt(request.getParameter("id"));
        UserMapperImpl userMapper=new UserMapperImpl();
        request.getSession().setAttribute("user",userMapper.selectUserById(id));
        String sele=request.getParameter("sele");
        if("sele".equals(sele)){
            request.getRequestDispatcher("select.jsp").forward(request,response);
        }
        else {
            request.getRequestDispatcher("update.html").forward(request,response);
        }

    }
}
